<template xmlns:v-bind="http://www.w3.org/1999/xhtml" xmlns:v-on="http://www.w3.org/1999/xhtml">
  <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="用户名" prop="name" style="margin-left: -27px;width: 340px">
      <el-input v-model="ruleForm.name"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="pass" style="margin-left: -27px;width: 340px">
      <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
    </el-form-item>
    <a href="#" class="a" style="padding-top: 4px;"   @click="dialogFormVisible1 = true"  >忘记密码？</a>
    <el-dialog title="找回密码" :visible.sync="dialogFormVisible1"  width="40%" >
      <back-password></back-password>
    </el-dialog>

    <el-form-item style="margin-left: -70px">
      <el-button type="primary" @click="submitForm('ruleForm')" :plain="true" >立即登录</el-button>

      <el-button  @click="dialogFormVisible = true"  style="margin-left:25px;">点击注册</el-button>
      <el-dialog title="欢迎注册" :visible.sync="dialogFormVisible"  width="40%" >
        <Register></Register>
      </el-dialog>
    </el-form-item>
  </el-form>
</template>

<script>
    import Register from "./Register";

    export default {
      name: "LoginByName",
      components:{
        Register: Register
      },
      data() {
        var checkName = (rule, value, callback) => {
          if (value === '') {
            return callback(new Error('用户名不能为空'));
          } else {
            callback();
          }
        };
        var validatePass = (rule, value, callback) => {
          if (value === '') {
            callback(new Error('请输入密码'));
          } else {
            callback();
          }
        };
        return {
          dialogFormVisible: false,
          dialogFormVisible1: false,
          ruleForm: {
            pass: '',
            name: '',
          },
          loginResult: {
            type: '',
            text: '',
          },
          rules: {
            pass: [
              {validator: validatePass, trigger: 'blur'}
            ],
            name: [
              {validator: checkName, trigger: 'blur'}
            ]
          }
        };
      },
      methods: {
        submitForm(formName) {
          this.$refs[formName].validate((valid) => {
             if (valid) {
               var data = {'username': this.ruleForm.name, 'password': this.ruleForm.pass};
               var url = '/user/LoginByName';
               this.$axios.post(url,data)
                 .then( (res)=> {
                   //document.write("data is coming", res.data.code);
                   if (res.data.code === 200) {
                     this.$message({ message: '欢迎'+res.data.data.name+'来到即刻运动！', type: 'success'});
                     //配置全局的username 和 userId
                     sessionStorage.setItem("username", res.data.data.name);
                     sessionStorage.setItem("userId", res.data.data.id);
                     sessionStorage.setItem("userPhone", res.data.data.phone);
                     //跳转至首页
                     this.$router.push({path:'/home'});
                   }
                   else {
                     this.$message({message: res.data.msg, type: 'error'})
                   }
                 }).catch(function (error) {
                 console.log(error);
               } );
             }
          });
        },
      }
    }

</script>

<style scoped>
  .a{
    text-decoration:none;
    font-size: 12px;
    position: absolute;
    top:105px;
    right: 10px;
    color: #333333;
  }
  .a:hover{color:red;}

</style>
